package com.eeepay.eeepay_v2.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.WebViewActivity;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.util.ImageLoaderUtils;
import com.eeepay.v2_library.util.ScreenSwitch;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.BannerProto;

/**
 * Created by ljq on 2016/2/29.
 * 销售中心头部广告适配器
 */
public class SaleAdvAdapter extends PagerAdapter{

    private List<BannerProto.BannerMsg> bannerMsgs;
    private Context context;

    public SaleAdvAdapter(Context context){
        this.context = context;
        this.bannerMsgs = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return bannerMsgs.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.sale_viewpager_item,container,false);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_sale_adv);
        ImageLoaderUtils.loadBitmap(bannerMsgs.get(position).bannerAttachment, imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tempLink = bannerMsgs.get(position).bannerLink;
                if(TextUtils.isEmpty(tempLink)){

                }else{
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.TITLE, bannerMsgs.get(position).bannerName);
                    bundle.putString(Constant.CANPS_QUERY, tempLink);
                    bundle.putString(Constant.INTENT_FLAG, Constant.CANPS_QUERY);
                    ScreenSwitch.switchActivity(context, WebViewActivity.class, bundle,-1);
                }

            }
        });
        container.addView(view);
        return view;
    }

    public void setDatas(List<BannerProto.BannerMsg> bannerMsgs){
        this.bannerMsgs.clear();
        this.bannerMsgs.addAll(bannerMsgs);
        this.notifyDataSetChanged();
    }

}
